#include "ToolBarBase.h"

#include "CFDPost/CFDPostOperatorsInterface/CFDPostGraphEventOperator.h"

#include "OperatorsInterface/GraphEventOperator.h"
#include "PostGraphAdaptor/PostGraphObjectBase.h"
#include "PostGraphAdaptor/PostGraphProperty.h"
#include "PostGraphAdaptor/PostGraphObjectManager.h"

#include "FITK_Interface/FITKInterfaceCFDPost/FITKAbstractCFDPostData.h"
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPost3DManager.h"
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPostData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "GUIFrame/MainWindow.h"
#include "SARibbonPannel.h"

namespace GUI {
    ToolBarBase::ToolBarBase(SARibbonPannel* pannel): m_pPannel(pannel) {
        _postDataManager = FITKAPP->getGlobalData()->getPostData<Interface::FITKCFDPostData>()->getPostDataManager();
        _postGraphManager = Interface::PostGraphObjectManager::getInstance();
        m_pMainWindow = dynamic_cast<GUI::MainWindow*>(FITKAPP->getGlobalData()->getMainWindow());
    }

    ToolBarBase::~ToolBarBase() {

    }

    QAction* ToolBarBase::createAction(QString actionName, QString iconPath, QString actionTitle) {
        if (actionName.isEmpty()) {
            return nullptr;
        }

        QAction* action = new QAction(m_pMainWindow);
        action->setObjectName(actionName);
        action->setIcon(QIcon(iconPath));
        action->setToolTip(actionTitle);
        connect(action, &QAction::triggered, this, [ = ]() {
            QObject* senderObject = sender();

            if (senderObject == nullptr) {
                return;
            }
            qDebug() << senderObject->objectName();
            auto acOper = Core::FITKOperatorRepo::getInstance()->getOperatorT<Core::FITKActionOperator>(senderObject->objectName());

            if (acOper == nullptr) {
                return;
            }

            acOper->setEmitter(senderObject);
            acOper->setArgs("objID", -1);
            acOper->actionTriggered();
        });
        return action;
    }

    QAction* ToolBarBase::createAction(QString iconText, QString actionName, QString iconPath, QString toolTip)
    {
        if (actionName.isEmpty()) {
            return nullptr;
        }

        QAction* action = new QAction(m_pMainWindow);
        action->setObjectName(actionName);
        action->setIcon(QIcon(iconPath));
        action->setToolTip(toolTip);
        action->setText(iconText);
        connect(action, &QAction::triggered, this, [=]() {
            QObject* senderObject = sender();

            if (senderObject == nullptr) {
                return;
            }
            qDebug() << senderObject->objectName();
            auto acOper = Core::FITKOperatorRepo::getInstance()->getOperatorT<Core::FITKActionOperator>(senderObject->objectName());

            if (acOper == nullptr) {
                return;
            }

            acOper->setEmitter(senderObject);
            acOper->setArgs("objID", -1);
            acOper->actionTriggered();
            });
        return action;
    }

    void ToolBarBase::reRender() {
        //EventOper::GraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("OperGraphPreprocess");
        EventOper::CFDPostGraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::CFDPostGraphEventOperator>("OperGraphPreprocess");

        if (graphOper == nullptr) {
            return;
        }

        //graphOper->reRender();
        graphOper->updateSingle(_currentDataID);
    }

    void ToolBarBase::addAction(QAction* action) {
        m_pMainWindow->pannelAddAction(m_pPannel, action, SARibbonPannelItem::Large);
        m_vActionList.push_back(action);
    }

    void ToolBarBase::addAction(QWidget* widget) {
        m_pPannel->addWidget(widget, SARibbonPannelItem::Small);
    }

    void ToolBarBase::setEnabled(bool enabled) {
        for (QAction* action : m_vActionList) {
            action->setEnabled(enabled);
        }
    }

    void ToolBarBase::on_actionClicked() {
        QObject* senderObject = sender();

        if (senderObject == nullptr) {
            return;
        }

        auto acOper = Core::FITKOperatorRepo::getInstance()->getOperatorT<Core::FITKActionOperator>(senderObject->objectName());

        if (acOper == nullptr) {
            return;
        }

        acOper->setEmitter(senderObject);
        acOper->setArgs("objID", -1);
        acOper->actionTriggered();
    }
}
